//
//  HRShowPowerControlView.m
//  Hear
//
//  Created by mac on 2017/9/1.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowPowerControlView.h"

@implementation HRShowPowerControlView

+(void)showPowerControlView:(NSString *)titleStr andContent:(NSString *)contentStr showImageName:(NSString *)imageName andBtnArr:(NSArray *)btnArr{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowPowerControlView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowPowerControlView class]]){
        
        
        HRShowPowerControlView *shareView = (HRShowPowerControlView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:titleStr andContent:contentStr showImageName:imageName andBtnArr:btnArr];
        
    }
}

- (void)loadView:(NSString *)titleStr andContent:(NSString *)contentStr showImageName:(NSString *)imageName andBtnArr:(NSArray *)btnArr{
    
    CGFloat contentViewWidth = CGHeightMT(300);
    
    CGFloat contentHeight = [contentStr boundingRectWithSize:CGSizeMake(contentViewWidth, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size.height+10;
    CGFloat contentViewHeight = 320+contentHeight;

    
    
    self.alpha = 0;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake((kFullWidth-contentViewWidth)/2, (kFullHeight-contentViewHeight)/2, contentViewWidth, contentViewHeight)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    contentView.alpha = 0;
    contentView.layer.cornerRadius = 4;
    
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1;
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    UIImageView *topImageView = [UIImageView new];
    topImageView.frame = CGRectMake(0, 0, contentView.width, 150);
    topImageView.image = [UIImage imageNamed:imageName];//@"权限显示图"
    topImageView.contentMode = UIViewContentModeScaleAspectFill;
    [contentView addSubview:topImageView];
    topImageView.layer.cornerRadius = 4;
    topImageView.clipsToBounds = YES;
    
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = titleStr;
    titleLabel.font = CustomUIFont(17);
    titleLabel.textColor = FirstTextColor;
    [contentView  addSubview:titleLabel];
    titleLabel.frame = CGRectMake(10,topImageView.bottom+20, contentView.width-20, 50);
    titleLabel.numberOfLines = 2;
    
    
    UILabel *contentLabel = [UILabel new];
    contentLabel.text = contentStr;
    contentLabel.font = CustomUIFont(14);
    contentLabel.textColor =SecondTextColor;
    [contentView  addSubview:contentLabel];
    contentLabel.frame = CGRectMake(titleLabel.left, titleLabel.bottom, titleLabel.width, contentView.height-titleLabel.bottom-40);
    contentLabel.numberOfLines = 0;

    
    UIButton *temButton;
    for (int i =0; i<btnArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:0];
        [button setTitle:btnArr[i] forState:0];
        button.titleLabel.font = CustomUIFont(17);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
        button.tag = i;
        if (btnArr.count==1){
            button.frame = CGRectMake(contentView.width-100, contentView.height-40, 80, 40);
            [button setTitleColor:MainColor forState:0];
        }else{
            if (i ==0){
                [button setTitleColor:FirstTextColor forState:0];
                button.frame = CGRectMake(contentView.width-60, contentView.height-40, 40, 40);
                temButton = button;
            }else{
                [button setTitleColor:MainColor forState:0];
                button.frame = CGRectMake(temButton.left-60, temButton.top, temButton.width, temButton.height);
            }
        }
    }
}

- (void)buttonClick:(UIButton *)button{
    if (button.tag==0){
        [self closeView];

    }else{
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            
            
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}



-(void)closeView
{
    self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
